﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:SYS_AUDITSET
	/// </summary>
	public partial class SYS_AUDITSET : BaseDAL<GSLMS.Entity.TB_SYS_AUDITSET>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public SYS_AUDITSET()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_SYS_AUDITSET model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_SPECAILTYPE", model.SPECAILTYPE),
								new OracleParameter("P_AUDITUSER", model.AUDITUSER),
								new OracleParameter("P_ORGID", model.ORGID),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE)};
  
			for (int i = 0; i < 6; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_audituser_add", parms);
                //清除缓存
                OracleHelper.RemoveCacheObject("_AuditUserList");
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_SYS_AUDITSET model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_SPECAILTYPE", model.SPECAILTYPE),
								new OracleParameter("P_AUDITUSER", model.AUDITUSER),
								new OracleParameter("P_ORGID", model.ORGID),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE)};
  
			for (int i = 0; i < 6; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_audituser_update", parms);
                //清除缓存
                OracleHelper.RemoveCacheObject("_AuditUserList");
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", ID)};
  
				parms[0].Direction = ParameterDirection.Input;

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_audituser_delete", parms);
                //清除缓存
                OracleHelper.RemoveCacheObject("_AuditUserList");
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

        /// <summary>
        /// 从缓存中获取审批用户信息
        /// </summary>
        /// <returns></returns>
        private DataTable GetAllByCache()
        {
            DataTable dt = null;
            if (OracleHelper.GetCachecObject("_AuditUserList") != null)
                dt = (DataTable)OracleHelper.GetCachecObject("_AuditUserList");
            if (dt == null || dt.Rows.Count == 0)
            {
                string sql = "";
                sql = "select TB_SYS_AUDITSET.*,(select orgname from tb_sys_org where id=TB_SYS_AUDITSET.orgid) orgname,(select empname from tb_sys_emp where username=TB_SYS_AUDITSET.audituser) EmpName from TB_SYS_AUDITSET ";
                DataSet ds = OracleHelper.ExecuteDataset(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    OracleHelper.AddCacheObject("_AuditUserList", (object)dt, 0);
                    //清除缓存
                    //OracleHelper.RemoveCacheObject("_AuditUserList");
                }

            }

            return dt;
        }


        /// <summary>
        /// 验证是否终审用户
        /// </summary>
        /// <param name="specailType">特批业务类型，1提前结清、2展期、费用减免、3还款计划变更、4放款审批,5特批业务</param>
        /// <param name="userName">需要验证的用户名</param>
        /// <returns></returns>
        public bool CheckIsLastAuditUser(int specailType,string userName)
        {
            bool flag = false;
            DataTable dt = OracleHelper.DataTableFileter(GetAllByCache(), " SpecailType=" + specailType.ToString() + " and AUDITUSER='" + userName + "'", "");
            if (dt != null && dt.Rows.Count > 0)
                flag = true;
            return flag;
        }


		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_SYS_AUDITSET DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_SYS_AUDITSET model = new GSLMS.Entity.TB_SYS_AUDITSET();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["SPECAILTYPE"]!=null && dr["SPECAILTYPE"].ToString()!="")
				{
					model.SPECAILTYPE=OracleHelper.ToInt(dr["SPECAILTYPE"].ToString());
				}
				if(dr["AUDITUSER"]!=null && dr["AUDITUSER"].ToString()!="")
				{
					model.AUDITUSER=dr["AUDITUSER"].ToString();
				}
				if(dr["ORGID"]!=null && dr["ORGID"].ToString()!="")
				{
					model.ORGID=OracleHelper.ToLong(dr["ORGID"].ToString());
				}
				if(dr["CREATEBY"]!=null && dr["CREATEBY"].ToString()!="")
				{
					model.CREATEBY=dr["CREATEBY"].ToString();
				}
				if(dr["CREATEDATE"]!=null && dr["CREATEDATE"].ToString()!="")
				{
					model.CREATEDATE=DateTime.Parse(dr["CREATEDATE"].ToString());
				}

                if (dr["OrgName"] != null && dr["OrgName"].ToString() != "")
                {
                    model.OrgName = dr["OrgName"].ToString();
                }

                if (dr["EmpName"] != null && dr["EmpName"].ToString() != "")
                {
                    model.EmpName = dr["EmpName"].ToString();
                }

				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_SYS_AUDITSET GetByID(long ID)
		{
			GSLMS.Entity.TB_SYS_AUDITSET sModel = new GSLMS.Entity.TB_SYS_AUDITSET();
			string sql = "";
            sql = "select TB_SYS_AUDITSET.*,(select orgname from tb_sys_org where id=TB_SYS_AUDITSET.orgid) orgname,(select empname from tb_sys_emp where username=TB_SYS_AUDITSET.audituser) EmpName from TB_SYS_AUDITSET where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_SYS_AUDITSET> GetList(GSLMS.Entity.TB_SYS_AUDITSET searchParam)
		{
			var list=new List<GSLMS.Entity.TB_SYS_AUDITSET>();
			DataTable dt = null;
			string tblName = "TB_SYS_AUDITSET";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
            string strFields = "TB_SYS_AUDITSET.*,(select orgname from tb_sys_org where id=TB_SYS_AUDITSET.orgid) orgname,(select empname from tb_sys_emp where username=TB_SYS_AUDITSET.audituser) EmpName";
			int totalCount = 0;
			int countFlag = 1;
			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_SYS_AUDITSET model = new GSLMS.Entity.TB_SYS_AUDITSET();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			
			return list;
		}

		#endregion  Method
	}
}

